﻿package kliment.data {
	import kliment.utils._Object;
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.2.8
	 * 
	 * class name: kliment.data.Table
	 */
	public dynamic class Table extends Array {
		private var _indexs:Array;
		
		public function Table(list:*, indexs:Array = null) {
			_indexs = indexs;
			_createTable(list);
			_createIndexs(this);
		}
		
		private function _createTable(list:*):void {
			var index:uint = 0;
			for each(var item:* in list) {
				this[index] = item;
				index++;
			}
		}
		
		private function _createIndexs(list:Array):void {
			var cell:Object;
			for each (var index:String in _indexs) {
				cell = this[index];
				if (!cell) {
					cell = new Object();
					this[index] = cell;
				}
				_createIndex(cell, list, index);
				_Object.setPropertyIsEnumerable(this, index, false);
			}
		}
		
		private function _createIndex(target:Object, list:Array, indexID:* = undefined):void {
			var key:*;
			var item:*;
			var index:uint = list.length;
			while (index--) {
				item = list[index];
				key = _getKey(item, indexID) || index;
				target[key] = item;
			}
		}
		
		private function _getKey(item:*, indexID:* = undefined):* {
			var result:*;
			if (indexID != undefined) 
				result = item[indexID];
			return result;
		}
		
		AS3 override function push(...rest):uint {
			_createIndexs(rest);
			return super.push.apply(this, rest);
		}
		
		AS3 override function concat(...rest):Array {
			for each (var res:* in rest)
				_createIndexs(res);
			return super.concat.apply(this, rest);
		}		
	}
}